package com.kai.steering.data.service.service.impl;

import com.kai.steering.data.service.entity.AnswerResult;
import com.kai.steering.data.service.entity.domainObject.AnswerDO;
import com.kai.steering.data.service.entity.domainObject.ClassificationDO;
import com.kai.steering.data.service.service.VerdictAnswerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@Slf4j
@Service
public class VerdictAnswerServiceImpl implements VerdictAnswerService {
    @Override
    public AnswerResult verdict(ClassificationDO classificationDO, AnswerDO checkAnswer, AnswerDO standardAnswer) {
        if (classificationDO.getClassificationId().equals("0001")){
            return choiceAnswer(checkAnswer,standardAnswer);
        }else if (classificationDO.getClassificationId().equals("0002")){
            return choiceAnswer(checkAnswer,standardAnswer);
        }
        else if (classificationDO.getClassificationId().equals("0003")){
            return choiceSAnswer(checkAnswer,standardAnswer);
        }
        else if (classificationDO.getClassificationId().equals("0004")){
            return choiceAnswer_004(checkAnswer,standardAnswer);
        }
        return null;
    }
    public AnswerResult choiceAnswer_004(AnswerDO checkAnswer, AnswerDO standardAnswer){
        AnswerResult answerResult = new AnswerResult(standardAnswer.getQuestionId(), standardAnswer.getAnswerId(),0);
        answerResult.setResult(100);
        return answerResult;
    }

    @Override
    public AnswerResult choiceAnswer(AnswerDO checkAnswer, AnswerDO standardAnswer) {

        String checkAnswerS = checkAnswer.getAnswer();
        String standardAnswerS = standardAnswer.getAnswer();
        AnswerResult answerResult = new AnswerResult(standardAnswer.getQuestionId(), standardAnswer.getAnswerId(),0);
        if (checkAnswerS.equals(standardAnswerS)) {
            answerResult.setResult(100);
            return answerResult;
        }
        return answerResult;
    }

    @Override
    public AnswerResult choiceSAnswer(AnswerDO checkAnswer, AnswerDO standardAnswer) {
        String checkAnswerS = checkAnswer.getAnswer();
        String standardAnswerS = standardAnswer.getAnswer();
        AnswerResult answerResult = new AnswerResult(standardAnswer.getQuestionId(), standardAnswer.getAnswerId(),0);

        //拆分去重
        String[] checkAnswerUA = StringUtils.splitByWholeSeparator(checkAnswerS, ",");
        Set<String> set = new HashSet<String>(Arrays.asList(checkAnswerUA));
        String[] checkAnswerA = set.toArray(new String[set.size()]);

        String[] standardAnswerA = StringUtils.splitByWholeSeparator(standardAnswerS, ",");
        int correct = 0;


        for (int i = 0; i < checkAnswerA.length; i++) {
            boolean existence = false;
            for (int j = 0; j < standardAnswerA.length; j++) {
                if (standardAnswerA[j].equalsIgnoreCase(checkAnswerA[i])) {
                    existence = true;
                    break;
                }
            }
            if (existence) {
                correct++;
            } else {
                return answerResult;
            }
        }
        double fraction = 1.00 * correct / standardAnswerA.length * 100;
        answerResult.setResult(fraction);
        return answerResult;
    }
}
